// 埋点相关方法

import cookie from 'vue-cookies'
/**
 * 处理参数
 * @param {obj}  to: 目标页面路由信息
 */
export const getPageInfo = (to) => {
  const pathName = to.path
  const query = to.query
  const pageInfo = {}
  if (pathName === '/index') {
    pageInfo.pageId = 1
  } else if (pathName === '/detail') {
    pageInfo.pageId = query.bannerEnter === '1' ? 2 : query.groupActivityId ? 5 : 3
    pageInfo.bizType = query.groupActivityId ? 1 : 0
    pageInfo.bizData = query.prodId
  } else if (pathName === '/secdetail-seckillId') {
    pageInfo.pageId = 4
    pageInfo.bizType = 2
    pageInfo.bizData = query.seckillId
  } else if (pathName === '/list') {
    pageInfo.pageId = 6
  } else if (pathName === '/special-discount') {
    pageInfo.pageId = 7
  } else if (pathName === '/cart') {
    pageInfo.pageId = 8
  } else if (pathName === '/order-detail') {
    pageInfo.pageId = 9
  } else if (pathName === '/payment') {
    pageInfo.pageId = 10
    pageInfo.bizData = sessionStorage.getItem('bbcPay_orderNumber')
  } else if (pathName === '/user-center') {
    pageInfo.pageId = 12
  } else if (pathName === '/user-center-uc-order') {
    pageInfo.pageId = 13
  }
  return pageInfo
}

/**
 * 获取唯一值
 */
export const getUuid = () => {
  const s = []
  const hexDigits = '0123456789abcdef'
  for (let i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
  }
  s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = '-'

  return s.join('')
}

export const saveLog = (flowData, visitType) => {
  const token = cookie.get('token')
  const flowAnalysisLogDto = Object.assign(flowData)
  flowAnalysisLogDto.visitType = visitType
  http({
    method: 'POST',
    url: token ? '/p/flowAnalysisLog' : '/flowAnalysisLog',
    data: flowAnalysisLogDto,
    baseURL: import.meta.env.VITE_APP_BASE_API,
    headers: {
      Authorization: token
    }
  })
}

/**
 * 记录页面点击次数
 */
export const tapLog = (visitType, prodNum) => {
  // 更新会话时间
  localStorage.setItem('bbcSessionTimeStamp', new Date().getTime())
  const flowAnalysisLogDto = JSON.parse(localStorage.getItem('bbcFlowAnalysisLogDto')) || {}
  flowAnalysisLogDto.visitType = visitType || 3
  flowAnalysisLogDto.nums = prodNum || undefined
  saveLog(flowAnalysisLogDto, visitType)
}
